package com.yanqu.road.server.cmd.activity.rankunionwar;

import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.RankUnionWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.*;

@Cmd(code = CrossProtocol.C3_CROSS_RANKUNIONWAR_GET_UNION_REQ, desc = "")
public class Cross3RankUnionWarGetUnionReqCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        RankUnionWarProto.CrossRankUnionWarGetMatchUnionReqMsg reqMsg = RankUnionWarProto.CrossRankUnionWarGetMatchUnionReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();

        List<String> unionUidList = null;
        unionUidList = getTopEarnSpeedUnion(reqMsg.getEarnSpeedTopCount(), new HashSet<>(reqMsg.getServerIdList()));
        if (unionUidList == null) {
            return;
        }
        // 返回值
        RankUnionWarProto.CrossRankUnionWarGetMatchUnionRespMsg.Builder builder = RankUnionWarProto.CrossRankUnionWarGetMatchUnionRespMsg.newBuilder();
        //来更新商会的
        if(reqMsg.getUnionIdList().size() > 0){
            builder.setIsUpdate(true);
            builder.setActivityId(activityId);
            for(String unionId : reqMsg.getUnionIdList()){
                CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionId);
                RankUnionWarProto.RankUnionWarUnionMsg.Builder tmp = RankUnionWarProto.RankUnionWarUnionMsg.newBuilder();
                tmp.setUnionUid(unionId);
                tmp.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
                builder.addUnion(tmp);
            }
            CrossUnionMgr.getLogger().error("巅峰商会战活动{}来更新商会信息,区服{},拉取{}个商会信息去更新,拉到{}个商会",activityId,reqMsg.getServerIdList(),reqMsg.getUnionIdCount(),builder.getUnionCount());
        }else{
            builder.setIsUpdate(false);
            builder.setActivityId(activityId);
            for (String s : unionUidList) {
                CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(s);
                Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(s);
                if (unionInfo != null && unionMemberMap != null) {
                    RankUnionWarProto.RankUnionWarUnionMsg.Builder tmp = RankUnionWarProto.RankUnionWarUnionMsg.newBuilder();
                    tmp.setUnionUid(s);
                    tmp.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
                    for (CrossUnionMember member : unionMemberMap.values()) {
                        tmp.addMember(RankUnionWarProto.RankUnionWarUnionMemberMsg.newBuilder()
                                .setUserId(member.getUserId())
                                .setServerId(member.getServerId())
                                .setPosition(member.getPosition())
                                .setEarnSpeed(member.getUserBaseInfo().getEarnSpeed().toString())
                        );
                    }
                    builder.addUnion(tmp);
                }
            }
            CrossUnionMgr.getLogger().error("巅峰商会战活动{}来拉取商会参加比赛,区服{},拉取前{}名商会,拉到{}个商会",activityId,reqMsg.getServerIdList(),reqMsg.getEarnSpeedTopCount(),builder.getUnionCount());
        }

        // 返回
        MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(CrossProtocol.C2_CROSS_RANKUNIONWAR_GET_UNION_RESP, builder));
    }

    @Override
    public void sendData() {

    }


    private List<String> getTopEarnSpeedUnion(int count, Set<Long> serverIdList) {
        // 获取活动范围内全部商会
        List<CrossUnionInfo> rankList = CrossUnionMgr.getUnionListByServerList(serverIdList);
        // 排序
        rankList.sort(new Comparator<CrossUnionInfo>() {
            @Override
            public int compare(CrossUnionInfo o1, CrossUnionInfo o2) {
                return o2.getTotalEarnSpeed().compareTo(o1.getTotalEarnSpeed());
            }
        });
        // 获取前几名
        List<String> list = new ArrayList<>();
        int rank = 0;
        for (CrossUnionInfo crossUnionInfo : rankList) {
            if (rank >= count) {
                break;
            }
            rank++;
            list.add(crossUnionInfo.getUnionUid());
        }
        return list;
    }

}
